package com.healthcare.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.healthcare.admin.pojo.User;
import com.healthcare.admin.mapper.UserMapper;
import com.healthcare.admin.query.UserQuery;
import com.healthcare.admin.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.healthcare.common.util.ExcelImportUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 新华医院转诊联盟
 * @since 2020-04-12
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired(required = false)
    private UserMapper userMapper;
    /**
     * 根据用户id删除
     * @param id
     */
    @Override
    public void deleteUserInfoById(Integer id) {
        userMapper.deleteById(id);
    }

    /**
     * 查询用户列表
     * @param pageParam
     * @param userQuery
     */
    @Override
    public void pageQuery(Page<User> pageParam, UserQuery userQuery) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("identity");

        if (userQuery == null){
            baseMapper.selectPage(pageParam, queryWrapper);
            return;
        }

        //医院名称
        String hospitalname = userQuery.getHospitalname();
        //科室名称
        String adminofficename = userQuery.getAdminofficename();


        if (!StringUtils.isEmpty(hospitalname)) {
            queryWrapper.like("Hospitaldictionaryname", hospitalname);
        }

        if (!StringUtils.isEmpty(adminofficename) ) {
            queryWrapper.like("adminofficename", adminofficename);
        }

        baseMapper.selectPage(pageParam, queryWrapper);
    }

    /**
     * 验证手机号是否唯一
     * @param mobile
     * @return
     */
    @Override
    public Integer selectMobile(String mobile) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        Integer price =  userMapper.selectCount(queryWrapper);
        return price;
    }
    /**
     * 验证只能有一个管理员
     * @param identity
     * @return
     */
    @Override
    public Integer selectAdministrator(Integer identity) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("identity",identity);
        Integer price =  userMapper.selectCount(queryWrapper);
        return price;
    }

    /**
     * 导入Excel
     * 分批量添加
     * @param file
     * @throws Exception
     */
    @Transactional(readOnly = false,rollbackFor = Exception.class)
    @Override
    public void batchImport(MultipartFile file) throws Exception {
        User user=null;

        //创建Excel工具类对象
        ExcelImportUtil excelHSSFUtil = new ExcelImportUtil(file.getInputStream());
        //获取工作表
        Sheet sheet = excelHSSFUtil.getSheet();

        //获取有记录的行数，即：最后有数据的行是第n行，前面有m行是空行没数据，则返回n-m；
        int rowCount = sheet.getPhysicalNumberOfRows();

        int rowCounter = rowCount-1;

        //如果行数小于等于1 提示错误 返回错误列表
        if (rowCount <= 1){
            System.out.println("请填写数据");

        }
        //根据行数遍历
        for (int rowNum = 1; rowNum <= rowCounter; rowNum++) {

            //获取行数据
            Row rowData = sheet.getRow(rowNum);
            //判断行数据是否为空
            if ( null != rowData) {// 行不为空
                Cell hospitaldictionaryname = rowData.getCell(0); // 医院名称
                Cell titledictionaryname = rowData.getCell(1);//职称名称
                Cell adminofficename = rowData.getCell(2);//科室名
                Cell mobile = rowData.getCell(3);//手机号
                Cell userpassword = rowData.getCell(4);//密码
                Cell username  = rowData.getCell(5);//用户姓名

                String  userName = excelHSSFUtil.getCellValue(hospitaldictionaryname).trim();
                String userPhone = excelHSSFUtil.getCellValue(titledictionaryname).trim();
                String  hospitalId = excelHSSFUtil.getCellValue(adminofficename).trim();
                String departmentId = excelHSSFUtil.getCellValue(mobile).trim();
                String  positionalId = excelHSSFUtil.getCellValue(userpassword).trim();
                String identity = excelHSSFUtil.getCellValue(username).trim();

                user=new User();
                user.setHospitaldictionaryname(userName);
                user.setTitledictionaryname(userPhone);
                user.setAdminofficename(hospitalId);
                user.setMobile(departmentId);
                user.setUserpassword(positionalId);
                user.setUsername(identity);
                user.setDeleted(false);
                user.setIdentity(1);
                userMapper.insert(user);
            }
        }
    }
}
